package com.demo.action;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import com.demo.entity.Humres;

@SuppressWarnings("unchecked")
public class GridAction{
	
	public void getGridData(HttpServletRequest request, HttpServletResponse response){
		JSONObject jo = new JSONObject();
		try{
			String index = request.getParameter("start");
			String pageSize = request.getParameter("limit");
			
			List<Humres> humresList = new ArrayList<Humres>();
			Humres h1 = new Humres();
			h1.setId("1");
			h1.setName("李涛");
			h1.setPhone("34456456");
			h1.setEmail("sdfsd@qq.com");
			humresList.add(h1);
			
			Humres h2 = new Humres();
			h2.setId("2");
			h2.setName("陆峰");
			h2.setPhone("34456456");
			h2.setEmail("sdfsd@qq.com");
			humresList.add(h2);
			
			Humres h3 = new Humres();
			h3.setId("3");
			h3.setName("方舟");
			h3.setPhone("34456456");
			h3.setEmail("sdfsd@qq.com");
			humresList.add(h3);
			
			Humres h4 = new Humres();
			h4.setId("4");
			h4.setName("文莱");
			h4.setPhone("34456456");
			h4.setEmail("sdfsd@qq.com");
			humresList.add(h4);
			
			Humres h5 = new Humres();
			h5.setId("5");
			h5.setName("杨云");
			h5.setPhone("34456456");
			h5.setEmail("sdfsd@qq.com");
			humresList.add(h5);
			
			Humres h6 = new Humres();
			h6.setId("6");
			h6.setName("张杰");
			h6.setPhone("34456456");
			h6.setEmail("sdfsd@qq.com");
			humresList.add(h6);
			
			Humres h7 = new Humres();
			h7.setId("7");
			h7.setName("朱小鲍");
			h7.setPhone("34456456");
			h7.setEmail("sdfsd@qq.com");
			humresList.add(h7);
			
			Humres h8 = new Humres();
			h8.setId("8");
			h8.setName("孙悟空");
			h8.setPhone("34456456");
			h8.setEmail("sdfsd@qq.com");
			humresList.add(h8);
			
			jo.put("humresData", doPagination(humresList, Integer.parseInt(index), Integer.parseInt(pageSize)));
			jo.put("totalRecord", humresList.size());
			jo.put("success", true);
			//System.out.println(jo);
			PrintWriter out = response.getWriter();
			out.print(jo.toString());
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	//list分页
    public List doPagination(List list, int index, int pageSize){
        List resultList = new ArrayList();
        for(int i = index; i < pageSize + index && i < list.size(); i++){
    		Humres h = (Humres)list.get(i);
        	resultList.add(h);
    	}
        return resultList;
    }
	
}